package oa.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import oa.biz.DepartmentBiz;
import oa.biz.EmployeeBiz;
import oa.entity.Department;
import oa.entity.Employee;

@Controller
@RequestMapping("/admin")
public class DepartmentController {
	@Autowired
	DepartmentBiz departmentBiz;
	@Autowired
	EmployeeBiz empBiz;
	@RequestMapping("/departmentList")
	public String departmentList(Model model){
		List<Employee> empList=empBiz.getAll();	
		List<Department> departmentList=departmentBiz.getAll();
		for(Department department:departmentList){
			for(Employee emp:empList){
				if(emp.getDepartmentId()==department.getId() && emp.getIsManager()==1){
					department.setDepartmentManager(emp.getName());
				}
		}
			
		}	
		model.addAttribute("departments",departmentList);	
		return "departmentList";
		
	}
	@RequestMapping("/getDepartment")
	@ResponseBody
	public List<Department> getDepartment(){
		return departmentBiz.getAll();
	}
	@RequestMapping("/getDepartmentztree")
	@ResponseBody
	public List<Object> getDepartmentztree(){
		List<Object> list = new ArrayList<Object>();
		 List<Department> departlist =  departmentBiz.getAllInLevel();
		 List<Employee> emplist= empBiz.getAllztree();
		 for(Department d :departlist){
			 list.add(d);
		 }
		 for(Employee e :emplist){
			 list.add(e);
		 }
		 return list;
	}
	@RequestMapping("/departmentSave")
	public String departmentSave(Department department){
		if(department.getId()==0){
			departmentBiz.addDepartment(department);
			return "redirect:departmentList";
			
		}else{
			departmentBiz.updateDepartment(department);
			return "redirect:departmentList";
		}
	}
	@RequestMapping("/getEmployeetree")
	@ResponseBody
	public List<Employee> getEmployeetree(){
		 List<Employee> emplist= empBiz.getAllztree();
		 return emplist;
	}
}
